<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        return "hello tppppp";
    }
    //显示登录页
    public function login()
    {
        return View();
    }

//登录提交
    public function doLogin()
    {
        //身份验证
        $status = input("status");
        $username = input("username");
        $password = input("password");
        $captcha = input("captcha");

        if (!captcha_check($captcha)) {
            //验证失败
            $ret = [
                'flag' => false,
                'message' => "验证码错误"
            ];
            return json($ret);//返回json
        }

        if ($status == "教师") {
            //验证教师的用户名密码是否正确
            $user = Db::name("teacher")
                ->where('username', $username)
                ->where('password', $password)
                ->find();
            //null, 0, "", "0", false, [], ''  没有属性的对象
            if (empty($user)) {
                $ret = [
                    'flag' => false,
                    'message' => '用户名或密码错误'
                ];
                return json($ret);
            } else {
                return ("登录成功lll");
            }
        } else if ($status == "学生") {
            $userr = Db::name("student")
                ->where('no', $username)
                ->where('password', $password)
                ->find();
            if (empty($userr)) {
                $ret = [
                    'flag' => false,
                    'message' => '用户名或密码错误'
                ];
                return json($ret);
            } else {
                return ("成功66666");
            }
        }
    }


//                使用session保存用户信息
//                session("user", $userr);



    //显示教师主页面
    public function zhuye()
    {
        return view();
    }
    //显示学生主页面
    public function stuye()
    {
        return view();
    }
    //数据接口：分页返回学生
    public function classcourse()
    {
        $page = input("page");//页号
        $limit = input("limit");//每条页数
        $offset = ($page - 1) * $limit;//偏移量

        //student总记录数
        $count=Db::name("classcourse")->count();

        //部分数据
        $arr = Db::name("classcourse")
            ->limit($offset, $limit)
            ->select()
            ->toArray();

        $data = [
            "code" => 0,
            "mes" => "",
            "count" => $count,//记录总数
            "data" => $arr,
        ];
        return json($data);
    }




//    public function hello($name = 'ThinkPHP6')
//    {
//        return 'hello,' . $name;
//    }
}
